<template>
  <el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
    <el-form :model="form" :rules="rules" :disabled="mode == 'show'" ref="dialogForm" label-width="100px" label-position="left">
      <el-form-item label="渠道名称" prop="name">
        <el-input v-model="form.name" clearable></el-input>
      </el-form-item>
      <el-form-item label="渠道类型" prop="type">
        <el-select v-model="form.type" filterable style="width: 100%" @change="platformChangeHandle">
          <el-option v-for="item in platforms" :key="item.type" :label="item.name" :value="item.type" />
        </el-select>
      </el-form-item>
      <el-form-item label="代理地址" prop="address">
        <el-input v-model="form.address" clearable></el-input>
      </el-form-item>
      <el-form-item label="秘钥" prop="key">
        <el-input v-model="form.key" clearable></el-input>
      </el-form-item>
      <el-form-item label="模型" prop="modelList">
        <el-select v-model="form.modelList" multiple filterable style="width: 100%" :allow-create="true">
          <el-option-group v-for="group in modelList" :key="group.code" :label="group.name">
            <el-option v-for="item in group.models" :key="item" :label="item" :value="item" />
          </el-option-group>
          <!-- <el-option
						v-for="item in modelList"
						:key="item"
						:label="item"
						:value="item"
					/> -->
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button v-if="mode != 'show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'AIChannelManageSave',
  emits: ['success', 'closed'],
  data() {
    return {
      mode: 'add',
      titleMap: {
        add: '新增',
        edit: '编辑',
        show: '查看',
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        id: '',
        address: '',
        type: '',
        name: '',
        key: '',
        modelList: [],
      },
      //验证规则
      rules: {
        name: [{ required: true, message: '请输入名称' }],
        type: [{ required: true, message: '请选择渠道类型' }],
        modelList: [{ required: true, message: '请选择模型' }],
      },
      modelList: [],
      // modelList: [
      // 	"test-1",
      // 	"qwen-turbo",
      // 	"glm-4v",
      // 	"glm-4",
      // 	"glm-4-flash",
      // 	"cogview-3-plus",
      // ],
      platforms: [],
    };
  },
  mounted() {
    this.loadAIPlatforms();
    this.loadAIModels();
  },
  methods: {
    async platformChangeHandle() {
      await this.loadAIModels();
    },
    async loadAIPlatforms() {
      let response = await this.$API.aiChannel.platforms.get();
      this.platforms = response.data;
    },
    async loadAIModels() {
      let response = await this.$API.aiModel.list.get(this.form.type);

      this.modelList = response.data;
    },
    //显示
    open(mode = 'add') {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          this.isSaveing = true;
          var res;
          if (this.form.id != '' && this.form.id != undefined && this.form.id != null) {
            res = await this.$API.aiChannel.update.post(this.form);
          } else res = await this.$API.aiChannel.create.post(this.form);
          this.isSaveing = false;
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode);
            this.visible = false;
            this.$message.success('操作成功');
          } else {
            this.$alert(res.message, '提示', { type: 'error' });
          }
        }
      });
    },
    //表单注入数据
    setData(data) {
      this.form.id = data.id;
      this.form.name = data.name;
      this.form.key = data.key;
      this.form.type = data.type;
      this.form.address = data.address;
      this.form.models = data.models;
      this.form.modelList = data.modelList;

      //可以和上面一样单个注入，也可以像下面一样直接合并进去
      //Object.assign(this.form, data)
    },
  },
};
</script>

<style>
.el-select__tags-text {
  min-height: 18px !important;
}
</style>
